﻿using System;
using System.Data.EntityClient;
using System.Data.SqlClient;
using System.Threading.Tasks;

namespace DAL
{
    public class DatabaseService
    {
        CRMEntities db;
        private static DatabaseService _Connection = null;
        private String _String = null;
        private static string _providerName = @"System.Data.SqlClient";
        private static string _serverName;
        private static string _databaseName;
        private static string _userId;
        private static string _password;

        public static string ConnectionString
        {
            get
            {
                if (_Connection == null)
                {
                    _Connection = new DatabaseService { _String = DatabaseService.Connect() };
                    return _Connection._String;
                }
                else
                    return _Connection._String;
            }
        }

        public System.Data.ConnectionState CheckConnection()
        {
            db = new CRMEntities();
            if (db.Database.Connection.State != System.Data.ConnectionState.Open)
            {
                try
                {
                    db.Database.Connection.Open();
                }
                catch
                {
                    db.Database.Connection.Dispose();
                }
                return db.Database.Connection.State;
            }
            else
            {
                return db.Database.Connection.State;
            }
        }

        public void GetConfig(System.Collections.Generic.Dictionary<string, string> config)
        {
            _serverName = config["serverName"].ToString().Trim();
            _databaseName = config["databaseName"].ToString().Trim();
            _userId = config["databaseUserId"].ToString().Trim();
            _password = config["databasePassWord"].ToString().Trim();
        }

        private static string Connect()
        { 
            string providerName = _providerName;
            string serverName = _serverName;
            string databaseName = _databaseName;
            string userId = _userId;
            string password = _password;

            // Initialize the connection string builder for the 
            // underlying provider.
            SqlConnectionStringBuilder sqlBuilder =
                new SqlConnectionStringBuilder();

            // Set the properties for the data source.
            sqlBuilder.DataSource = serverName;
            sqlBuilder.InitialCatalog = databaseName;
            sqlBuilder.IntegratedSecurity = false;
            sqlBuilder.UserID = userId;
            sqlBuilder.Password = password;
            sqlBuilder.MultipleActiveResultSets = true;
            sqlBuilder.TrustServerCertificate = true;

            // Build the SqlConnection connection string. 
            string providerString = sqlBuilder.ToString();

            // Initialize the EntityConnectionStringBuilder.
            EntityConnectionStringBuilder entityBuilder =
                new EntityConnectionStringBuilder();

            //Set the provider name.
            entityBuilder.Provider = providerName;

            // Set the provider-specific connection string.
            entityBuilder.ProviderConnectionString = providerString;           

            // Set the Metadata location.
            entityBuilder.Metadata = "res://*/CRMData.csdl|res://*/CRMData.ssdl|res://*/CRMData.msl";

            return entityBuilder.ToString();

        }


    }
}
